package com.freedom.rpc.thrift.common.machine;

import java.util.Properties;

import com.freedom.rpc.thrift.common.loadbalancer.LoadBalancer;
import com.freedom.rpc.thrift.common.loadbalancer.RoundRobinLoadBalancer;
import com.freedom.rpc.thrift.common.loadbalancer.WeightedResponseTimeLoadBalancer;
import com.freedom.rpc.thrift.common.property.ClientProperties;
import com.freedom.rpc.thrift.common.utils.Logger;

public class LoadBalancerHelper {
	private static final Logger logger = Logger.getLogger(LoadBalancerHelper.class);
	// 静态区域
	private static String LOAD_BALANCER = "loadbalancer";
	private static String ROUND_ROBIN = "roundrobin";
	private static String RESPONSE_TIME = "responsetime";
	// 单例
	private static LoadBalancer instance = null;
	static {
		Properties properties = ClientProperties.getInstance();
		String strategy = properties.getProperty(LOAD_BALANCER, ROUND_ROBIN);
		if (strategy.equals(ROUND_ROBIN)) {
			instance = new RoundRobinLoadBalancer();
			logger.info("load balance strategy---" + ROUND_ROBIN);
		} else if (strategy.equals(RESPONSE_TIME)) {
			instance = new WeightedResponseTimeLoadBalancer();
			logger.info("load balance strategy---" + RESPONSE_TIME);
		} else {
			instance = new RoundRobinLoadBalancer();// 给一个默认值
			logger.info("load balance strategy default ---" + ROUND_ROBIN);
		}
	}

	public static LoadBalancer getInstance() {
		return instance;
	}
}
